package com.zrrd.mybatisplusFirst.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zrrd.mybatisplusFirst.entity.Teacher;
import com.zrrd.mybatisplusFirst.service.TeacherService;
import com.zrrd.mybatisplusFirst.utils.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author WanJl
 * @version 1.0
 * @title TeacherController
 * @description
 * @create 2024/3/30
 */
@RestController
@RequestMapping("/teachers")
public class TeacherController {
    @Resource
    TeacherService teacherService;

    @GetMapping
    @ResponseBody
    public ResponseResult queryAll(){
        List<Teacher> list = teacherService.list();


        return ResponseResult.queryResponse(list);
    }
    @GetMapping("/queryPassWord")
    @ResponseBody
    public ResponseResult queryPassWord(){
        QueryWrapper lqw=new QueryWrapper<>();
        lqw.select("password");
        List<Teacher> list = teacherService.list(lqw);
        return ResponseResult.queryResponse(list);

    }

    /**
     * 根据id列表批量删除
     * @param list
     * @return
     */
    @PostMapping("/removeByIds")
    @ResponseBody
    public ResponseResult removeByIds(@RequestBody List<Integer> list){
        boolean flag = teacherService.removeByIds(list);
        return ResponseResult.removeResponse(flag);
    }

    @PutMapping
    @ResponseBody
    public ResponseResult update(@RequestBody Teacher teacher){
        //先查询
        Teacher teacher1 = teacherService.getById(teacher.getId());
        teacher.setVersion(teacher1.getVersion());
        //再修改
        boolean flag = teacherService.updateById(teacher);
        return ResponseResult.updateResponse(flag);
    }

}
